import os

from kitool import ApiKit as api
from kitool.DateKit import get_datestr
from kitool.ExcelKit import read_excel


def tranfer_txt(resp, prop):
    x = resp.get(prop)
    if x is None:
        return ''
    x_list = []
    for item in x:
        x_status = item.get('status')
        if x_status is not None:
            tag = 'check' if '1' == x_status else 'cross'
            x_list.append(f"{item.get('name')}&{tag};")
    # join
    return ' '.join(x_list)


def append_nlp(url, some, str_list):
    req_data = {'text': some}
    resp_some = api.post(url, req_data)
    symptom = tranfer_txt(resp_some, 'symptom')
    sign = tranfer_txt(resp_some, 'sign')
    label = tranfer_txt(resp_some, 'label')

    str_list.append('|NLP|')
    str_list.append('|---|')
    str_list.append('|`symptom` ' + symptom + ' |')
    str_list.append('|`sign` ' + sign + ' |')
    str_list.append('|`label` ' + label + ' |\n\n')


def tranfer_person(p, nlp_url, top_icon):
    str_list = []
    str_list.append('# :' + top_icon + ': ' + str(p.id) + '\n')

    for title, content in zip(['主诉', '现病史', '体格检查', '辅助检查'], [p.zs, p.xbs, p.tg, p.fz]):
        str_list.append('## ' + title + ' \n')
        str_list.append('> ' + content + '\n')
        append_nlp(nlp_url, content, str_list)
    # return
    return '\n'.join(str_list)



def valid_excel_to_gen_md(func):
    conf_common = func('common')
    work_dir = conf_common['work_dir']
    conf_md = func('md')
    nlp_url = conf_md['nlp_url']
    top_icon = conf_md['top_icon']
    valid_excel_name = conf_md['valid_excel_name']
    df = read_excel(work_dir, valid_excel_name)
    md_full_out_path = os.path.join(work_dir, get_datestr(), get_datestr() + f'__{valid_excel_name}.md')
    for index, row in df.iterrows():
        if '×' == row['验证'].strip():
            str_list = []
            text = row['病情描述']
            str_list.append(f"# :{top_icon}: {index} ")
            str_list.append('> ' + text)
            append_nlp(nlp_url, text, str_list)
            result_str = '\n'.join(str_list)
            # out
            with open(md_full_out_path, 'a', encoding='UTF-8') as file:
                file.write(result_str + '\n\n\n\n\n')



def valid_excel_to_gen_md_by_api(df, func):
    conf_common = func('common')
    work_dir = conf_common['work_dir']
    conf_md = func('md')
    nlp_url = conf_md['nlp_url']
    top_icon = conf_md['top_icon']
    valid_excel_name = conf_md['valid_excel_name']
    # df = read_excel(work_dir, valid_excel_name)
    md_full_out_path = os.path.join(work_dir, get_datestr(), get_datestr() + f'__{valid_excel_name}.md')
    for index, row in df.iterrows():
        if '×' == row['验证'].strip():
            str_list = []
            text = row['病情描述']
            str_list.append(f"# :{top_icon}: {index} ")
            str_list.append('> ' + text)
            append_nlp(nlp_url, text, str_list)
            result_str = '\n'.join(str_list)
            # out
            with open(md_full_out_path, 'a', encoding='UTF-8') as file:
                file.write(result_str + '\n\n\n\n\n')